package com.zang.greedy;

/**
 * @author ZVerify
 * @since 2022/11/20 18:07
 * @see <a href="https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/">...</a>
 *
 **/
public class 买买股票的最佳时机II {

    // 局部最优
    public int maxProfit(int[] prices) {

        int sum = 0;
        for (int i = 0; i < prices.length-1; i++) {
            // 预知未来还愁赔钱???
            // 如果第二天比第一天股值高那就今天卖明天买
            if (prices[i+1]-prices[i]>0) sum+= prices[i+1]-prices[i];
        }
        return sum;
    }

    // 动态规划
    public int maxProfit(int[] prices, int fee) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        int length = prices.length;

        int[][] dp = new int[length][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0] ;// 当前第一次买还没有卖出肯定赔钱
        for (int i = 1; i < length; i++) {
            // 没有股票的情况
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
            // 持有股票的情况
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i] );
        }
        return dp[length - 1][0];
    }

}
